package com.upcode.weighing.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.upcode.weighing.domain.AppInfo;
import com.upcode.weighing.exception.AdminHandlerException;
import com.upcode.weighing.mapper.AppInfoMapper;
import com.upcode.weighing.service.AppInfoService;
import com.upcode.weighing.vo.req.AppEidtionRecordReqVo;
import com.upcode.weighing.vo.req.PageReqVo;
import com.upcode.weighing.vo.req.pc.AppEidtionRecordVo;
import com.upcode.weighing.vo.resp.PageRespVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author alex
 * @since 2024-07-21
 */
@Service
public class AppInfoServiceImpl extends ServiceImpl<AppInfoMapper, AppInfo> implements AppInfoService {
    private static final Logger logger = LoggerFactory.getLogger(AppInfoServiceImpl.class);

    @Autowired
    private AppInfoMapper appInfoMapper;

    /**
     *根据条件查询APP版本列表
     * @param vo
     * @return
     */
    @Override
    public PageRespVo<AppEidtionRecordVo> queryPageList(PageReqVo<AppEidtionRecordReqVo> vo, String appPackDownloadPath) {
        long currentPage = vo.getPage().getCurrentPage();
        long pageSize = vo.getPage().getPageSize();
        long from = (currentPage-1)*pageSize;
        long count = appInfoMapper.queryPageCount(vo.getParams());
        List<AppEidtionRecordVo> list = appInfoMapper.queryPageList(from,pageSize,vo.getParams());
//        if(null != list && list.size() > 0){
//            for(AppEidtionRecordVo appPack :list){
//                if(StringUtils.isNotBlank(appPack.getOrigName())){
//                    appPack.setDownloadPath(appPackDownloadPath+appPack.getOrigName());
//                }
//            }
//        }
        IPage<AppEidtionRecordVo> iPage = new Page<>(currentPage,pageSize,count);
        iPage.setRecords(list);
        PageRespVo<AppEidtionRecordVo> pageRespVo = new PageRespVo<>();
        pageRespVo.setPage(iPage);
        return pageRespVo;
    }

    /**
     * 启用禁用APP版本
     * @param id
     * @param status
     * @return
     */
    @Transactional(noRollbackFor = AdminHandlerException.class)
    @Override
    public boolean enableAppEidtion( String id, String status, String serverUrl, String profile) {
        if("0".equals(status)){
            appInfoMapper.updateAppEidtionStatus(id);
        }
        Integer updateNum = appInfoMapper.enableAppEidtion(id,status);
        if(null == updateNum || updateNum == 0){
            return false;
        }
        return true;
    }


    /**
     * 删除APP版本信息
     * @param id
     * @return
     */
    @Override
    public boolean deleteAppEidtion( String id ) {
        AppInfo data = appInfoMapper.selectById(id);
        if(null == data){
            throw new RuntimeException("APP版本信息不存在");
        }
        if(StringUtils.isNotEmpty(data.getDownloadPath())){
            File file = new File(data.getDownloadPath());
            if (file.exists()) {
                file.delete();
            }
        }
        Integer delNum = appInfoMapper.deleteById(id);
        if(null == delNum || delNum == 0){
            return false;
        }
        return true;
    }
}
